from time import sleep

from base.get_driver import GetDriver
from base.get_logger import GetLogger
from page.page_cart import PageCart
import pytest
from page.page_login import PageLogin

log = GetLogger().get_logger()

class TestCart():
    #新建setup_class
    def setup_class(self):
        try:
            #实例化driver
            self.driver = GetDriver().get_driver()
            #实例化PageCart类
            self.cart = PageCart(self.driver)
            #调用登录成功
            PageLogin(self.driver).page_login_success()
            #回到首页
            self.cart.page_open_index()
        except Exception as e:
            log.error("错误:{}".format(e))
            self.cart.base_get_image()

    #新建teardown_class
    def teardown_class(self):
        GetDriver().quit_driver()

    #新建购物车测试方法
    def test_cart(self):
        try:
            #调用添加购物车组装方法
            sleep(5)
            self.cart.add_cart("小米手机")
            #断言添加成功
            msg = self.cart.page_get_add_result()
            print("msg:", msg)
            assert msg == "添加成功"
            #关闭窗口
            self.cart.page_close_window()
        except Exception as e:
            log.error("错误:{}".format(e))
            self.cart.base_get_image()


